package com.bjc.platform.vo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonBackReference;

/**
 * 封装为Ext可直接使用的数据结构
 * 
 */
public class ExtTreeModel {

	// Sql语句用到的常量
	public static final String CODE = "code";
	public static final String ICON = "icon";
	public static final String ID = "id"; // 组织ID
	public static final String NAME = "name"; // 组织名称
	public static final String PARENT_ID = "parentId"; // 组织名称
	public static final String HAS_CHILDREN = "hasChildren"; // 是否有子节点
	public static final String CHECKED = "checked"; // 是否有子节点

	private String cls; // 节点显示的图标Css名称
	private boolean leaf;// 节点是否叶子节点
	private String text; // 节点显示名称
	private Object data; // 业务数据
	private Boolean checked; // 复选框
	private List<ExtTreeModel> children; // 当前节点下的子节点
	private boolean expanded; // 是否展开当前节点
	private String root;// 根据节点名称
	private int id; // 当前节点ID
	private ExtTreeModel parent; // 当前节点父

	public String getCls() {
		return cls;
	}

	public void setCls(String cls) {
		this.cls = cls;
	}

	public boolean getLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public List<ExtTreeModel> getChildren() {
		return children;
	}

	public void setChildren(List<ExtTreeModel> children) {
		this.children = children;
	}

	public boolean isExpanded() {
		return expanded;
	}

	public void setExpanded(boolean expanded) {
		this.expanded = expanded;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@JsonBackReference
	public ExtTreeModel getParent() {
		return parent;
	}

	public void setParent(ExtTreeModel parent) {
		this.parent = parent;
	}

	/*
	 * 将组织信息体例封装为Ext的树形结构
	 */
	public static ExtTreeModel assamblyTreeStructForExt(
			List<Map<String, Object>> orgs, boolean isChecked,
			boolean pIsChecked, boolean expanded) {
		Object name = null;
		Object pid = null;
		Object id = null;
		Object hasChildren = null;
		Object checked = null;

		// 创建ExtTree的数据
		ExtTreeModel root = new ExtTreeModel();
		List<ExtTreeModel> rootChildren = new ArrayList<ExtTreeModel>();
		root.setChildren(rootChildren);
		ExtTreeModel treeModelTmp = null;
		ExtTreeModel pNodeTmp = null;
		List<ExtTreeModel> tmpChildren = null;
		for (Map<String, Object> m : orgs) {
			id = m.get(ID);
			pid = m.get(PARENT_ID);
			name = m.get(NAME);
			hasChildren = m.get(HAS_CHILDREN);
			checked = m.get(CHECKED);

			treeModelTmp = new ExtTreeModel();
			treeModelTmp.setText(String.valueOf(name));
			treeModelTmp.setExpanded(expanded);
			treeModelTmp.setData(m);
			treeModelTmp.setId(Integer.valueOf(String.valueOf(id)));

			if (hasChildren == null) {
				setItemChecked(isChecked, checked, treeModelTmp);
				treeModelTmp.setLeaf(true);
			} else {
				if (pIsChecked) {
					setItemChecked(isChecked, checked, treeModelTmp);
				}
				tmpChildren = new ArrayList<ExtTreeModel>();
				treeModelTmp.setChildren(tmpChildren);
			}

			if (pid == null) {
				root.getChildren().add(treeModelTmp);
			} else {
				pNodeTmp = ExtTreeModel.getExtTreeModelById(root,
						Integer.valueOf(String.valueOf(pid)));
				treeModelTmp.setParent(pNodeTmp);
				if (pNodeTmp == null) {
					root.getChildren().add(treeModelTmp);
				} else {
					pNodeTmp.getChildren().add(treeModelTmp);
				}
			}
		}
		return root;
	}

	private static void setItemChecked(boolean isChecked, Object checked,
			ExtTreeModel treeModelTmp) {
		if (isChecked) {
			if (checked != null && "checked".equals(String.valueOf(checked))) {
				treeModelTmp.setChecked(true);
			} else {
				treeModelTmp.setChecked(false);
			}
		}
	}

	/*
	 * 要ExtTreeModel中查询指定ID节点实例
	 * 
	 * @return
	 */
	private static ExtTreeModel getExtTreeModelById(ExtTreeModel tm, int pid) {
		List<ExtTreeModel> children = tm.getChildren();
		ExtTreeModel parent = null;
		if (tm.getId() == pid) {
			parent = tm;
		} else {
			if (tm.getChildren() != null && tm.getChildren().size() > 0) {
				for (ExtTreeModel m : children) {
					if (parent == null) {
						parent = getExtTreeModelById(m, pid);
					}
				}
			}
		}

		return parent;
	}
}
